'''解析网页内容'''

from bs4 import BeautifulSoup
import requests

map = {}
for i in range(1, 5):
    # print(i)
    resp = requests.get(f'http://books.toscrape.com/catalogue/page-{i}.html')
    content = resp.text
    # print(content)
    # 解析html
    soup = BeautifulSoup(content, 'html.parser')
    # p标签的数据
    # print(soup.p)

    # 找到所有p标签下class属性为price_color的元素 获取价格
    # all_price = soup.findAll("p", attrs={"class": "price_color"})
    # for price in all_price:
    # string为获取p标签内的数据
    # print(f'价格 -> {price.string[2:]}')

    # 获取书名
    index = 0
    all_book_name_h3 = soup.findAll("h3")
    str = ""
    for book_name_h3 in all_book_name_h3:
        all_price = soup.findAll("p", attrs={"class": "price_color"})
        book_name = book_name_h3.find("a")
        str += f'{book_name.string} -> £{all_price[index].string[2:]}\n'
        # print(f'{book_name.string} -> £{all_price[index].string[2:]}')
        index += 1
    map[i] = str
for key in map.keys():
    print(f'第【{key}】页 数据：\n{map[key]}')
